﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using ExcelCOM = Microsoft.Office.Interop.Excel;

namespace LanAnh
{
    public partial class frmQLCongDan : Office2007Form 
    {
        public frmQLCongDan(string phanquyen)
        {
            Status.Init();
            InitializeComponent();
            quyenhan = phanquyen;
        }
        string quyenhan = "";
        Data data = new Data();
        MyDataPaging paging;
        string them = "";
        string sql;
        string lblNoiDungClick;
        string lblCellClick;
        private void kiemTraCongDanViPham(string maCongDan)
        {
            //int count_NghiDinh163;
            //int count_ViPhamHanhChinh;
            //int count_GiaoDuc;
            //int count_DiTu;
            //int count_TuTha;
            ////sql = "SELECT COUNT(cMaCongDan) FROM tblNghiDinh163 WHERE cMaCongDan=N'"+maCongDan +"'";
            ////count_NghiDinh163 = int.Parse(data.executeQueryTable(sql).Rows[0][0].ToString());
            ////sql = "SELECT COUNT(cMaCongDan) FROM tblViPhamHanhChinh WHERE cMaCongDan=N'" + maCongDan + "'";
            ////count_ViPhamHanhChinh=int.Parse(data.executeQueryTable(sql).Rows[0][0].ToString());
            ////sql = "SELECT COUNT(cMaCongDan) FROM tblGiaoDuc WHERE cMaCongDan=N'" + maCongDan + "'";
            ////count_GiaoDuc=int.Parse(data.executeQueryTable(sql).Rows[0][0].ToString());
            ////sql = "SELECT COUNT(cMaCongDan) FROM tblDiTu WHERE cMaCongDan=N'" + maCongDan + "'";
            ////count_DiTu=int.Parse(data.executeQueryTable(sql).Rows[0][0].ToString());
            ////sql = "SELECT COUNT(cMaCongDan) FROM tblTuTha WHERE cMaCongDan=N'" + maCongDan + "'";
            ////count_TuTha = int.Parse(data.executeQueryTable(sql).Rows[0][0].ToString());

            //if (count_NghiDinh163 == 0 && count_ViPhamHanhChinh == 0 && count_GiaoDuc == 0 && count_DiTu==0 && count_TuTha==0) //Công Dân Gương Mẫu Không vi phạm gì 
            //{
            //    //expandablePanelThongTin.Visible  = false ;
            //    btnCoTienAn.Visible = false ;
            //}
            //else
            //{
            //   // expandablePanelThongTin.Visible  = true;
            //    btnCoTienAn.Visible = true;
            //}
            
        
        }
        private void btnThem_Click(object sender, EventArgs e)
        {
            GridViewdata.ReadOnly = true;
            navigationPane1.Expanded = true;
            them = "them";
            btnThem.Enabled = false ;
            btnXoa.Enabled = false ;
            btnSua.Enabled =false ;
            btnLuu.Enabled = true ;
            txtHoTen.ReadOnly = false;
            txtChuyenCaHo.ReadOnly = false;
            txtCMND.ReadOnly = false;
            txtAp.ReadOnly = false;
            txtDanToc.ReadOnly = false;
            txtDiDau.ReadOnly = false;
            txtGhiChu.ReadOnly = false;
            txtNgayDen.ReadOnly = false;
            txtNgayDi.ReadOnly = false;
            txtNgaySinh.ReadOnly = false;
            txtNgheNghiep.ReadOnly = false;
            txtNguyenQuan.ReadOnly = false;
            txtNoiSinh.ReadOnly = false;
            txtODauDen.ReadOnly = false;
            txtQuanHeChuHo.ReadOnly = false;
            cbAp.Enabled = false;
            txtSoHoKhau.ReadOnly = false;
            txtSoNha.ReadOnly = false;
            txtSoSoHoKhau.ReadOnly = false;
            txtTonGiao.ReadOnly = false;
            txtTrinhDoHocVan.ReadOnly = false;
            cbGioiTinh.Enabled = true   ;
            GridViewdata.Enabled = false;
            lbMaCongDan.Text = DateTime.Now.Day.ToString()+ DateTime.Now.Month.ToString()+DateTime.Now.Year.ToString()+DateTime.Now.Hour.ToString()+DateTime.Now.Minute.ToString()+DateTime.Now.Second.ToString()+DateTime.Now.Millisecond.ToString();
            txtHoTen.Focus();

            txtHoTen.Text  = "";
            txtChuyenCaHo.Text = "";
            txtCMND.Text = "";
            txtDanToc.Text = "";
            txtDiDau.Text = "";
            txtGhiChu.Text = "";
            txtNgayDen.Text = "";
            txtAp.Text = cbAp.Text;
            txtNgayDi.Text = "";
            txtNgaySinh.Text = "";
            txtNgheNghiep.Text = "";
            txtNguyenQuan.Text = "";
            txtNoiSinh.Text = "";
            txtODauDen.Text = "";
            txtQuanHeChuHo.Text = "";
            txtSoHoKhau.Text = "";
            txtSoNha.Text = "";
            txtSoSoHoKhau.Text = "";
            txtTonGiao.Text = "";
            txtTrinhDoHocVan.Text = "";

        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            GridViewdata.ReadOnly = true;
            string macongdan = "";
            if (GridViewdata.SelectedRows.Count > 1)
            {
                if (MessageBoxEx.Show("Bạn có muốn xóa công dân đã được chọn không ?", "DELETE", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                {
                    for (int i = 0; i < GridViewdata.SelectedRows.Count; i++)
                    {
                        try
                        {
                            macongdan = GridViewdata.Rows[GridViewdata.SelectedRows[i].Index].Cells["cMaCongDan"].Value.ToString();
                            sql = "Delete From tblCongDan Where cMaCongDan= N'" + macongdan + "'";
                            data.executeNonQuery(sql);
                            
                        }
                        catch { }
                    }
                    paging.Refresh();
                    //btnRefresh_Click(sender, e);
                }
            }
            else if (MessageBoxEx.Show("Bạn có muốn xóa Công Dân '" + txtHoTen.Text + "' này không ?", "DELETE", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                try
                {
                    sql = "Delete From tblCongDan Where cMaCongDan= N'" + lbMaCongDan.Text  + "'";
                    data.executeNonQuery(sql);
                    btnRefresh_Click(sender, e);
                }
                catch
                {
                    MessageBoxEx.Show("Công dân này không thể xóa !");
                }

            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            GridViewdata.ReadOnly = false;
            navigationPane1.Expanded = true;
            them = "sua";
            checkChet.Visible = true;
            btnThem.Enabled = false ;
            btnXoa.Enabled = false ;
            btnSua.Enabled = false ;
            btnLuu.Enabled = true ;
            cbAp.Enabled = false ;
            txtAp.ReadOnly = false;
            txtHoTen.ReadOnly = false;
            txtChuyenCaHo.ReadOnly = false;
            txtCMND.ReadOnly = false;
            txtDanToc.ReadOnly = false;
            txtDiDau.ReadOnly = false;
            txtGhiChu.ReadOnly = false;
            txtNgayDen.ReadOnly = false;
            txtNgayDi.ReadOnly = false;
            txtNgaySinh.ReadOnly = false;
            txtNgheNghiep.ReadOnly = false;
            txtNguyenQuan.ReadOnly = false;
            txtNoiSinh.ReadOnly = false;
            txtODauDen.ReadOnly = false;
            txtQuanHeChuHo.ReadOnly = false;
            txtSoHoKhau.ReadOnly = false;
            txtSoNha.ReadOnly = false;
            txtSoSoHoKhau.ReadOnly = false;
            txtTonGiao.ReadOnly = false;
            txtTrinhDoHocVan.ReadOnly = false;
            cbGioiTinh.Enabled = true ;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            GridViewdata.ReadOnly = true;
            if (them == "them")
            {
                try
                {
                    if (txtHoTen.Text.Replace(" ", "") == "")
                    {
                        MessageBoxEx.Show("Nhập Tên !");
                        txtHoTen.Focus();
                    }
                    else 
                    {
                        sql = "INSERT INTO tblCongDan(cMaCongDan,cAp,cHoTen,cQHChuHo,cGioiTinh,cNgaySinh,cNguyenQuan,cNoiSinh,cSoCMND,cDanToc,cSoNha,cTrinhDoHocVan,cTonGiao,cNgheNghiep,cSoHoKhau,cSoSoHoKhau,cODauDen,cNgayDen,cDiDau,cNgayDi,cChuyenDiCaHo,cGhiChu) VALUES(N'" + lbMaCongDan.Text + "',N'"+txtAp.Text+"',N'" + txtHoTen.Text + "',N'" + txtQuanHeChuHo.Text + "',N'" + cbGioiTinh.Text + "',N'" + txtNgaySinh.Text + "',N'" + txtNguyenQuan.Text + "',N'" + txtNoiSinh.Text + "',N'" + txtCMND.Text + "',N'" + txtDanToc.Text + "',N'" + txtSoNha.Text + "',N'" + txtTrinhDoHocVan.Text + "',N'" + txtTonGiao.Text + "',N'" + txtNgheNghiep.Text + "',N'" + txtSoHoKhau.Text + "',N'" + txtSoSoHoKhau.Text + "',N'" + txtODauDen.Text + "',N'" + txtNgayDen.Text + "',N'" + txtDiDau.Text + "',N'" + txtNgayDi.Text + "',N'" + txtChuyenCaHo.Text + "',N'" + txtGhiChu.Text + "')";
                        data.executeNonQuery(sql);
                        btnRefresh_Click(sender, e);
                        btnLuu.Enabled = false;
                        btnThem_Click(sender, e);
                    }
       

                }catch{}
            }
            if (them == "sua")
            {
                sql = "UPDATE tblCongDan SET cHoTen=N'" + txtHoTen.Text + "',cAp=N'"+txtAp.Text +"',cQHChuHo=N'" + txtQuanHeChuHo.Text + "',cGioiTinh=N'" + cbGioiTinh.Text + "',cNgaySinh=N'" + txtNgaySinh.Text + "',cNguyenQuan=N'" + txtNguyenQuan.Text + "',cNoiSinh=N'" + txtNoiSinh.Text + "',cSoCMND=N'" + txtCMND.Text + "',cDanToc=N'" + txtDanToc.Text + "',cSoNha=N'" + txtSoNha.Text + "',cTrinhDoHocVan= N'" + txtTrinhDoHocVan.Text + "',cTonGiao=N'" + txtTonGiao.Text + "',cNgheNghiep=N'" + txtNgheNghiep.Text + "',cSoHoKhau=N'" + txtSoHoKhau.Text + "',cSoSoHoKhau=N'" + txtSoSoHoKhau.Text + "',cODauDen=N'" + txtODauDen.Text + "',cNgayDen=N'" + txtNgayDen.Text + "',cDiDau=N'" + txtDiDau.Text + "',cNgayDi=N'" + txtNgayDi.Text + "',cChuyenDiCaHo=N'" + txtChuyenCaHo.Text + "',cGhiChu=N'" + txtGhiChu.Text + "', cChet=N'"+checkChet.Checked.ToString() +"' WHERE cMaCongDan='"+lbMaCongDan.Text +"' ";                   
                data.executeNonQuery(sql);
                btnRefresh_Click(sender, e);
            }
            
            GridViewdata.Enabled = true ;
            
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            GridViewdata.ReadOnly = true;
            them = "";
            checkChet.Visible = false;
            btnThem.Enabled = true;
            btnXoa.Enabled = true;
            btnSua.Enabled = true;
            btnLuu.Enabled = false;
            txtHoTen.ReadOnly = true;
            txtAp.ReadOnly  = true ;
            cbAp.Enabled = true;
            txtChuyenCaHo.ReadOnly = true;
            txtCMND.ReadOnly = true;
            txtDanToc.ReadOnly = true;
            txtDiDau.ReadOnly = true;
            txtGhiChu.ReadOnly = true;
            txtNgayDen.ReadOnly = true;
            txtNgayDi.ReadOnly = true;
            txtNgaySinh.ReadOnly = true;
            txtNgheNghiep.ReadOnly = true;
            txtNguyenQuan.ReadOnly = true;
            txtNoiSinh.ReadOnly = true;
            txtODauDen.ReadOnly = true;
            txtQuanHeChuHo.ReadOnly = true;
            txtSoHoKhau.ReadOnly = true;
            txtSoNha.ReadOnly = true;
            txtSoSoHoKhau.ReadOnly = true;
            txtTonGiao.ReadOnly = true;
            txtTrinhDoHocVan.ReadOnly = true ;
            cbGioiTinh.Enabled = false;
            GridViewdata.Enabled = true ;
            if (quyenhan == "Chỉ Xem")
            {
                btnThem.Visible = false;
                btnXoa.Visible = false;
                btnSua.Visible = false;
                btnLuu.Visible = false;
            }
            else if (quyenhan == "Xem - Thêm - Chỉnh Sửa")
            {
                btnXoa.Visible = false;
            }
            //data.HienThiComboBox(cbAp, "tblCongDan", "cAp", "cAp");
            String sApCu = cbAp.Text;
            cbAp.DataSource = data.executeQueryTable("SELECT distinct cAp FROM tblCongDan");
            cbAp.DisplayMember = "cAp" ;
            cbAp.ValueMember = "cAp" ;
            cbAp.Text = sApCu;

            data.autoComplete(cbAp, "tblCongDan", "cAp");
            data.autoComplete(txtAp , "tblCongDan", "cAp");
            data.autoComplete(txtQuanHeChuHo, "tblCongDan", "cQHChuHo");
            //data.autoComplete(txtDanToc, "tblCongDan", "cDanToc");
            //data.autoComplete(txtTonGiao, "tblCongDan", "cTonGiao");
            data.autoComplete(txtNguyenQuan, "tblCongDan", "cNguyenQuan");
            data.autoComplete(txtNoiSinh, "tblCongDan", "cNoiSinh");
            data.autoComplete(txtSoNha, "tblCongDan", "cSoNha");
            data.autoComplete(txtTrinhDoHocVan, "tblCongDan", "cTrinhDoHocVan");
            data.autoComplete(txtNgheNghiep, "tblCongDan", "cNgheNghiep");
            data.autoComplete(txtCMND, "tblCongDan", "cSoCMND");

            
            //sql = "select * from tblCongDan";
            //paging = new MyDataPaging("tblCongDan", sql, "", "cHoTen", 50, GridViewdata, txtTrangHienTai, lbTongSoTrang);            
            //paging.Home();
            kiemTraCongDanViPham(lbMaCongDan.Text);
            cbAp_SelectedIndexChanged(sender, e);
            
        }

        private void frmQLCongDan_Load(object sender, EventArgs e)
        {
            btnRefresh_Click(sender, e);
            hienthimauGridview();
        }

        private void GridViewdata_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                
                lbMaCongDan.Text = GridViewdata.Rows[e.RowIndex].Cells["cMaCongDan"].Value.ToString();
                txtHoTen.Text = GridViewdata.Rows[e.RowIndex].Cells["cHoTen"].Value.ToString();
                txtAp.Text = GridViewdata.Rows[e.RowIndex].Cells["cAp"].Value.ToString();
                txtChuyenCaHo.Text = GridViewdata.Rows[e.RowIndex].Cells["cChuyenDiCaHo"].Value.ToString();
                txtCMND.Text = GridViewdata.Rows[e.RowIndex].Cells["cSoCMND"].Value.ToString();
                txtDanToc.Text = GridViewdata.Rows[e.RowIndex].Cells["cDanToc"].Value.ToString();
                txtDiDau.Text = GridViewdata.Rows[e.RowIndex].Cells["cDiDau"].Value.ToString();
                txtGhiChu.Text = GridViewdata.Rows[e.RowIndex].Cells["cGhiChu"].Value.ToString();
                txtNgayDen.Text = GridViewdata.Rows[e.RowIndex].Cells["cNgayDen"].Value.ToString();
                txtNgayDi.Text = GridViewdata.Rows[e.RowIndex].Cells["cNgayDi"].Value.ToString();
                txtNgaySinh.Text = GridViewdata.Rows[e.RowIndex].Cells["cNgaySinh"].Value.ToString();
                txtNgheNghiep.Text = GridViewdata.Rows[e.RowIndex].Cells["cNgheNghiep"].Value.ToString();
                txtNguyenQuan.Text = GridViewdata.Rows[e.RowIndex].Cells["cNguyenQuan"].Value.ToString();
                txtNoiSinh.Text = GridViewdata.Rows[e.RowIndex].Cells["cNoiSinh"].Value.ToString();
                txtODauDen.Text = GridViewdata.Rows[e.RowIndex].Cells["cODauDen"].Value.ToString();
                txtQuanHeChuHo.Text = GridViewdata.Rows[e.RowIndex].Cells["cQuanHeChuHo"].Value.ToString();
                txtSoHoKhau.Text = GridViewdata.Rows[e.RowIndex].Cells["cSoHoKhau"].Value.ToString();
                txtSoNha.Text = GridViewdata.Rows[e.RowIndex].Cells["cSoNha"].Value.ToString();
                txtSoSoHoKhau.Text = GridViewdata.Rows[e.RowIndex].Cells["cSoSoHoKhau"].Value.ToString();
                txtTonGiao.Text = GridViewdata.Rows[e.RowIndex].Cells["cTonGiao"].Value.ToString();
                txtTrinhDoHocVan.Text = GridViewdata.Rows[e.RowIndex].Cells["cTrinhDo"].Value.ToString();
                cbGioiTinh.Text = GridViewdata.Rows[e.RowIndex].Cells["cGioiTinh"].Value.ToString();
                kiemTraCongDanViPham(lbMaCongDan.Text );
                string chet=GridViewdata.Rows[e.RowIndex].Cells["Chet"].Value.ToString();
                if (chet.ToLower() == "true")
                {
                    //expandablePanelThongTin.Visible   = true;
                    lbThongBao.Visible = true;
                    lbThongBao.Text = "Công dân này đã chết.";
                }
                else
                {
                    lbThongBao.Visible = false;
                    //expandablePanelThongTin.Visible  = false ;
                }
            }catch{}
        }
        
        private void cbSoItemTrenTrang_SelectedIndexChanged(object sender, EventArgs e)
        {
            paging.Update(Int32.Parse(cbSoItemTrenTrang.Text));
            paging.Home();
        }

        private void cbSoItemTrenTrang_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                paging.Update(Int32.Parse(cbSoItemTrenTrang.Text));
                paging.Home();    
            }
        }

        private void bindingNavigatorMoveFirstItem_Click(object sender, EventArgs e)
        {
            paging.Home();
        }

        private void bindingNavigatorMovePreviousItem_Click(object sender, EventArgs e)
        {
            paging.Previous();
        }

        private void bindingNavigatorMoveNextItem_Click(object sender, EventArgs e)
        {
            paging.Next();
        }

        private void bindingNavigatorMoveLastItem_Click(object sender, EventArgs e)
        {
            paging.End();
        }

        private void txtTrangHienTai_KeyDown(object sender, KeyEventArgs e)
        {
            //if (txtTrangHienTai.Text.Replace(" ", "") == "")
            //    txtTrangHienTai.Text = "1";
            //if(e.KeyCode==Keys.Enter )
            //    paging.Curent();
        }

        private void txtHoTen_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter||e.KeyCode==Keys.Down )
            {
                cbGioiTinh.Focus();
            }
        }

        private void txtQuanHeChuHo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Down)
            {
                txtSoSoHoKhau.Focus();
            }
            if (e.KeyCode == Keys.Up)
            {
                txtQuanHeChuHo.Focus();
            }
        }

        private void cbGioiTinh_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Down)
            {
                txtNgaySinh.Focus();
            }
            if (e.KeyCode == Keys.Up)
            {
                txtQuanHeChuHo.Focus();
            }
        }

        private void txtNgaySinh_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Down)
            {
                txtNguyenQuan.Focus();
            }
            if (e.KeyCode == Keys.Up)
            {
                cbGioiTinh.Focus();
            }
        }

        private void txtNguyenQuan_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Down)
            {
                txtNoiSinh.Focus();
            }
            if (e.KeyCode == Keys.Up)
            {
                txtNgaySinh.Focus();
            }
        }

        private void txtNoiSinh_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Down)
            {
                txtCMND.Focus();
            }
            if (e.KeyCode == Keys.Up)
            {
                txtNguyenQuan.Focus();
            }
        }

        private void txtCMND_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Down)
            {
                txtDanToc.Focus();
            }
            if (e.KeyCode == Keys.Up)
            {
                txtNoiSinh.Focus();
            }
        }

        private void txtDanToc_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Down)
            {
                txtAp.Focus();
            }
            if (e.KeyCode == Keys.Up)
            {
                txtCMND.Focus();
            }
        }

        private void txtSoNha_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Down)
            {
                txtTrinhDoHocVan.Focus();
            }
            if (e.KeyCode == Keys.Up)
            {
                txtAp.Focus();
            }
        }

        private void txtTrinhDoHocVan_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Down)
            {
                txtTonGiao.Focus();
            }
            if (e.KeyCode == Keys.Up)
            {
                txtSoNha.Focus();
            }
        }

        private void txtTonGiao_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Down)
            {
                txtNgheNghiep.Focus();
            }
            if (e.KeyCode == Keys.Up)
            {
                txtTrinhDoHocVan.Focus();
            }
        }

        private void txtNgheNghiep_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Down)
            {
                txtSoHoKhau.Focus();
            }
            if (e.KeyCode == Keys.Up)
            {
                txtTonGiao.Focus();
            }
        }

        private void txtSoHoKhau_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Down)
            {
                txtQuanHeChuHo.Focus();
            }
            if (e.KeyCode == Keys.Up)
            {
                txtNgheNghiep.Focus();
            }
        }

        private void txtSoSoHoKhau_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Down)
            {
                txtODauDen.Focus();
            }
            if (e.KeyCode == Keys.Up)
            {
                txtSoHoKhau.Focus();
            }
        }

        private void txtODauDen_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Down)
            {
                txtNgayDen.Focus();
            }
            if (e.KeyCode == Keys.Up)
            {
                txtSoSoHoKhau.Focus();
            }
        }

        private void txtNgayDen_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Down)
            {
                txtDiDau.Focus();
            }
            if (e.KeyCode == Keys.Up)
            {
                txtODauDen.Focus();
            }
        }

        private void txtDiDau_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Down)
            {
                txtNgayDi.Focus();
            }
            if (e.KeyCode == Keys.Up)
            {
                txtNgayDen.Focus();
            }
        }

        private void txtNgayDi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Down)
            {
                txtChuyenCaHo.Focus();
            }
            if (e.KeyCode == Keys.Up)
            {
                txtDiDau.Focus();
            }
        }

        private void txtChuyenCaHo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Down)
            {
                txtGhiChu.Focus();
            }
            if (e.KeyCode == Keys.Up)
            {
                txtNgayDi.Focus();
            }
        }

        private void txtGhiChu_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Down)
            {
                btnLuu.Focus();
            }
            if (e.KeyCode == Keys.Up)
            {
                txtChuyenCaHo.Focus();
            }
        }

        private void GridViewdata_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F3 || e.KeyCode == Keys.Control || e.KeyCode == Keys.F)
            {
                GridViewdata.ReadOnly = true;
                frmTimKiem frmTim = new frmTimKiem("tblCongDan", lblCellClick, lblNoiDungClick);
                frmTim.passData = new frmTimKiem.PassData(GetFirstValue);
                frmTim.ShowDialog();                
            }
            if (e.KeyCode == Keys.F10)
            {
                GridViewdata.ReadOnly = false;
                MessageBox.Show("Mở chức năng chỉnh sửa");
            }
        }

        public void GetFirstValue(String value)
        {
            paging.Update(value);
            paging.Home();
        }

        private void GridViewdata_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                lblCellClick = GridViewdata.Columns[e.ColumnIndex].DataPropertyName.ToString();
                lblNoiDungClick = GridViewdata.SelectedCells[0].Value.ToString();
            }
            catch { }
        }

        private void GridViewdata_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            GridViewdata_CellClick(sender, e);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            btnThem_Click(sender, e);
        }

        private void btnRefesh_Click(object sender, EventArgs e)
        {
            btnRefresh_Click(sender, e);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            btnXoa_Click(sender, e);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            btnLuu_Click(sender, e);
        }

       
        
        
        
        //------------- In Lý Lịch-------------------------------
        private void btnInLyLich_Click(object sender, EventArgs e)
        {
            //if (MessageBoxEx.Show("Bạn có muốn In Lý Lịch Của Công Dân  '" + txtHoTen.Text + "'  này không ?", "THÔNG BÁO", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
            //{
                System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument();
                pd.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(pd_PrintPage);
                pd.PrintController = new System.Drawing.Printing.StandardPrintController();
                try
                {
                    pd.Print();
                }
                catch
                {
                    MessageBox.Show("Không thể kết nối với máy in", "Lỗi");
                }
            //}
        }
        
        private void pd_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs ev)
        {
            DataTable dtViPhamHanhChinh;
            String thongbao = "";
            int dongmoi = 0;
            int x=10, y=10;
            Font printFont = new Font("Tahoma", 10, FontStyle.Regular  );
            Font printFontBold = new Font("Tahoma", 9, FontStyle.Regular);
            Font Tieude = new Font("Tahoma", 10, FontStyle.Bold);
            Font BangLyLich = new Font("Tahoma", 14, FontStyle.Bold);
            Graphics g = ev.Graphics;
            g.DrawString("CÔNG AN HUYỆN VĨNH CỬU", printFont, Brushes.Black, x + 50, y);
            g.DrawString("CÔNG AN XÃ TÂN BÌNH", printFont, Brushes.Black, x + 60, y+20);
            
            g.DrawString("CỘNG HÒA XÃ HỘI CHỦ NGHĨA VIỆT NAM", Tieude, Brushes.Black, x + 270, y);
            g.DrawString("ĐỘC LẬP - TỰ DO - HẠNH PHÚC", printFont, Brushes.Black, x + 300, y+20);

            //g.DrawLine(Pens.Black, 0, y + 25, x + 580, y + 35);
            g.DrawString("Tân Bình, Ngày  "+DateTime.Now.Day.ToString()+"    Tháng  "+DateTime.Now.Month.ToString()+"  Năm "+DateTime.Now.Year.ToString()+"", printFont, Brushes.Black, x + 450, y + 50);

            g.DrawString("BẢNG LÝ LỊCH", BangLyLich , Brushes.Black, x + 300, y + 80);
            g.DrawString("Họ Tên: "+txtHoTen.Text +"", printFont, Brushes.Black, x + 30, y + 120);
            g.DrawString("Ngày, tháng, năm sinh: " + txtNgaySinh.Text  + "", printFont, Brushes.Black, x + 30, y + 140);
            g.DrawString("Giới tính: " + cbGioiTinh.Text  + "", printFont, Brushes.Black, x + 30, y + 160);
            g.DrawString("Nguyên quán: " + txtNguyenQuan.Text  + "", printFont, Brushes.Black, x + 30, y + 180);
            g.DrawString("Nơi sinh: " + txtNoiSinh.Text   + "", printFont, Brushes.Black, x + 30, y + 200);
            g.DrawString("Số CMND: " + txtCMND.Text  + "", printFont, Brushes.Black, x + 30, y + 220);
            g.DrawString("Dân tộc: " + txtDanToc.Text  + "", printFont, Brushes.Black, x + 30, y + 240);
            g.DrawString("Nơi đăng ký hộ khẩu thường trú: " + txtSoNha.Text  + "", printFont, Brushes.Black, x + 30, y + 260);
            g.DrawString("Trình độ học vấn: " + txtTrinhDoHocVan.Text  + "", printFont, Brushes.Black, x + 30, y + 280);
            g.DrawString("Tôn giáo: " + txtTonGiao.Text  + "", printFont, Brushes.Black, x + 30, y + 300);
            g.DrawString("Nghề nghiệp: " + txtNgheNghiep.Text  + "", printFont, Brushes.Black, x + 30, y + 320);
            g.DrawString("Ở đâu chuyển đến: " + txtODauDen.Text  + "", printFont, Brushes.Black, x + 30, y + 340);
            g.DrawString("Đến ngày: " + txtNgayDen.Text  + "", printFont, Brushes.Black, x + 30, y + 360);
            g.DrawString("Chuyển đi đâu: " + txtDiDau.Text   + "", printFont, Brushes.Black, x + 30, y + 380);
            g.DrawString("Đi ngày: " + txtNgayDi.Text + "", printFont, Brushes.Black, x + 30, y + 400);
            
            
           
            // In vi pham hanh chinh
            sql = "SELECT * FROM tblViPhamHanhChinh WHERE cMaCongDan=N'" + lbMaCongDan.Text  + "'";
            dtViPhamHanhChinh = data.executeQueryTable(sql);            
            string chuoi = "";            
            
            if (dtViPhamHanhChinh.Rows.Count == 0)
            {

                thongbao = "Chưa có vi phạm.";
                g.DrawString("Tiền sự: " + thongbao + "", printFont, Brushes.Black, x + 30, y + 420);
            }
            else
            {
                g.DrawString("Tiền sự:", printFont, Brushes.Black, x + 30, y + 420);
                for (int i = 0; i < dtViPhamHanhChinh.Rows.Count; i++)
                {
                     chuoi = "     Ngày " + dtViPhamHanhChinh.Rows[i]["cNgayViPham"].ToString() + ", vi phạm " + dtViPhamHanhChinh.Rows[i]["cHanhVi"].ToString() + ", được " + dtViPhamHanhChinh.Rows[i]["cCoQuanXuLy"].ToString() + ", xử lý " + dtViPhamHanhChinh.Rows[i]["cHinhThucXuLy"].ToString() + "";
                     int cat = 100;
                     int temp = 0;
                     bool isCat = false;
                     if (chuoi.Length > cat)
                     {
                         for (int j = cat; j > 0; j--)
                         {
                             isCat = false;
                             if (chuoi[j] == ' ')
                             {
                                 //in ra o day
                                 String Out = chuoi.Substring(0, cat - temp);
                                 //bo cua teo vo
                                 g.DrawString(Out, printFont, Brushes.Black, x + 50, y + 440 + dongmoi);
                                 dongmoi = dongmoi + 20;                                 
                                 if (chuoi.Length - (cat - temp) > 0)
                                 {
                                     chuoi = chuoi.Substring(cat - temp, chuoi.Length - (cat - temp));
                                     if (chuoi.Length < cat)
                                         break;
                                     isCat = true;
                                     j = cat;
                                     temp = 0;
                                 }
                             }
                             if (!isCat)
                                 temp++;
                         }
                     }
                     else
                     {
                         g.DrawString(chuoi, printFont, Brushes.Black, x + 50, y + 440 + dongmoi);
                         dongmoi = dongmoi + 20;
                     }                
                }
            }
            
            //In vi pham 163
            g.DrawString("Tiền Án:", printFont, Brushes.Black, x + 30, y + 440 + dongmoi);
            dongmoi = dongmoi + 20;

            sql = "SELECT * FROM tblNghiDinh163 WHERE cMaCongDan=N'" + lbMaCongDan.Text + "'";
            dtViPhamHanhChinh = data.executeQueryTable(sql);
            

            if (dtViPhamHanhChinh.Rows.Count == 0)
            {
                
            }
            else
            {
                g.DrawString("Nghị Định 163:", printFont, Brushes.Black, x + 30, y+ 440 + dongmoi );
                dongmoi = dongmoi + 20;
                for (int i = 0; i < dtViPhamHanhChinh.Rows.Count; i++)
                {
                    chuoi = "     Ngày thực hiện " + dtViPhamHanhChinh.Rows[i]["cNgayThucHienQD"].ToString() + ", vi phạm " + dtViPhamHanhChinh.Rows[i]["cViPham"].ToString() + "";
                    int cat = 100;
                    int temp = 0;
                    bool isCat = false;
                    if (chuoi.Length > cat)
                    {
                        for (int j = cat; j > 0; j--)
                        {
                            isCat = false;
                            if (chuoi[j] == ' ')
                            {
                                //in ra o day
                                String Out = chuoi.Substring(0, cat - temp);
                                //bo cua teo vo
                                g.DrawString(Out, printFont, Brushes.Black, x + 50, y + 440 + dongmoi);
                                dongmoi = dongmoi + 20;
                                if (chuoi.Length - (cat - temp) > 0)
                                {
                                    chuoi = chuoi.Substring(cat - temp, chuoi.Length - (cat - temp));
                                    if (chuoi.Length < cat)
                                        break;
                                    isCat = true;
                                    j = cat;
                                    temp = 0;
                                }
                            }
                            if (!isCat)
                                temp++;
                        }
                    }
                    else
                    {
                        g.DrawString(chuoi, printFont, Brushes.Black, x + 50, y + 440 + dongmoi);
                        dongmoi = dongmoi + 20;
                    }
                }
            }

            //In vo co so giao duc truong giao duong

            sql = "SELECT * FROM tblGiaoDuc WHERE cMaCongDan=N'" + lbMaCongDan.Text + "'";
            dtViPhamHanhChinh = data.executeQueryTable(sql);

            if (dtViPhamHanhChinh.Rows.Count == 0)
            {
                
            }
            else
            {
                g.DrawString("Vào cơ sở giáo dục - trường giáo dưỡng:", printFont, Brushes.Black, x + 30, y + 440 + dongmoi);
                dongmoi = dongmoi + 20;
                for (int i = 0; i < dtViPhamHanhChinh.Rows.Count; i++)
                {
                    chuoi = "     Ngày vào " + dtViPhamHanhChinh.Rows[i]["cNgayVaoGiaoDuc"].ToString() + ", vi phạm " + dtViPhamHanhChinh.Rows[i]["cHanhVi"].ToString() + " thời gian vào là " + dtViPhamHanhChinh.Rows[i]["cThoiGianChapHanh"].ToString() + "";
                    int cat = 100;
                    int temp = 0;
                    bool isCat = false;
                    if (chuoi.Length > cat)
                    {
                        for (int j = cat; j > 0; j--)
                        {
                            isCat = false;
                            if (chuoi[j] == ' ')
                            {
                                //in ra o day
                                String Out = chuoi.Substring(0, cat - temp);
                                //bo cua teo vo
                                g.DrawString(Out, printFont, Brushes.Black, x + 50, y + 440 + dongmoi);
                                dongmoi = dongmoi + 20;
                                if (chuoi.Length - (cat - temp) > 0)
                                {
                                    chuoi = chuoi.Substring(cat - temp, chuoi.Length - (cat - temp));
                                    if (chuoi.Length < cat)
                                        break;
                                    isCat = true;
                                    j = cat;
                                    temp = 0;
                                }
                            }
                            if (!isCat)
                                temp++;
                        }
                    }
                    else
                    {
                        g.DrawString(chuoi, printFont, Brushes.Black, x + 50, y + 440 + dongmoi);
                        dongmoi = dongmoi + 20;
                    }
                }
            }

            // In đi tù
            sql = "SELECT * FROM tblDiTu WHERE cMaCongDan=N'" + lbMaCongDan.Text + "'";
            dtViPhamHanhChinh = data.executeQueryTable(sql);

            if (dtViPhamHanhChinh.Rows.Count == 0)
            {
                
            }
            else
            {
                g.DrawString("Đi Tù:", printFont, Brushes.Black, x + 30, y + 440 + dongmoi);
                dongmoi = dongmoi + 20;
                for (int i = 0; i < dtViPhamHanhChinh.Rows.Count; i++)
                {
                    chuoi = "     Ngày vào " + dtViPhamHanhChinh.Rows[i]["cNgayBiBat"].ToString() + ", tội danh " + dtViPhamHanhChinh.Rows[i]["cToiDanh"].ToString() + ", thời gian chấp hành " + dtViPhamHanhChinh.Rows[i]["cThoiGianChapHanh"].ToString() + "";
                    int cat = 100;
                    int temp = 0;
                    bool isCat = false;
                    if (chuoi.Length > cat)
                    {
                        for (int j = cat; j > 0; j--)
                        {
                            isCat = false;
                            if (chuoi[j] == ' ')
                            {
                                //in ra o day
                                String Out = chuoi.Substring(0, cat - temp);
                                //bo cua teo vo
                                g.DrawString(Out, printFont, Brushes.Black, x + 50, y + 440 + dongmoi);
                                dongmoi = dongmoi + 20;
                                if (chuoi.Length - (cat - temp) > 0)
                                {
                                    chuoi = chuoi.Substring(cat - temp, chuoi.Length - (cat - temp));
                                    if (chuoi.Length < cat)
                                        break;
                                    isCat = true;
                                    j = cat;
                                    temp = 0;
                                }
                            }
                            if (!isCat)
                                temp++;
                        }
                    }
                    else
                    {
                        g.DrawString(chuoi, printFont, Brushes.Black, x + 50, y + 440 + dongmoi);
                        dongmoi = dongmoi + 20;
                    }
                }
            }
            //In Tù tha đặc xá
            sql = "SELECT * FROM tblTuTha WHERE cMaCongDan=N'" + lbMaCongDan.Text + "'";
            dtViPhamHanhChinh = data.executeQueryTable(sql);

            if (dtViPhamHanhChinh.Rows.Count == 0)
            {

            }
            else
            {
                g.DrawString("Ra Tù:", printFont, Brushes.Black, x + 30, y + 440 + dongmoi);
                dongmoi = dongmoi + 20;
                for (int i = 0; i < dtViPhamHanhChinh.Rows.Count; i++)
                {
                    chuoi = "     Ngày vào " + dtViPhamHanhChinh.Rows[i]["cNgayBiBat"].ToString() + ", tội danh " + dtViPhamHanhChinh.Rows[i]["cToiDanh"].ToString() + ", ngày ra tù " + dtViPhamHanhChinh.Rows[i]["cNgayThaVe"].ToString() + "";
                    int cat = 100;
                    int temp = 0;
                    bool isCat = false;
                    if (chuoi.Length > cat)
                    {
                        for (int j = cat; j > 0; j--)
                        {
                            isCat = false;
                            if (chuoi[j] == ' ')
                            {
                                //in ra o day
                                String Out = chuoi.Substring(0, cat - temp);
                                //bo cua teo vo
                                g.DrawString(Out, printFont, Brushes.Black, x + 50, y + 440 + dongmoi);
                                dongmoi = dongmoi + 20;
                                if (chuoi.Length - (cat - temp) > 0)
                                {
                                    chuoi = chuoi.Substring(cat - temp, chuoi.Length - (cat - temp));
                                    if (chuoi.Length < cat)
                                        break;
                                    isCat = true;
                                    j = cat;
                                    temp = 0;
                                }
                            }
                            if (!isCat)
                                temp++;
                        }
                    }
                    else
                    {
                        g.DrawString(chuoi, printFont, Brushes.Black, x + 50, y + 440 + dongmoi);
                        dongmoi = dongmoi + 20;
                    }
                }
                g.DrawString("TRƯỞNG CÔNG AN XÃ", printFont, Brushes.Black, x + 300, y + 440+dongmoi );
            }

            

        }

        private void txtCMND_Validated(object sender, EventArgs e)
        {
            if (txtCMND.Text.Replace("  ", "") == "")
            { }
            else
            {
                if (txtCMND.AutoCompleteCustomSource.Contains(txtCMND.Text))
                {
                    txtCMND.Text = "Số CMND này đã có,Số CMND không được trùng với người khác !";
                    txtCMND.Focus();
                }
            }
        }

        private void cbAp_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dtChonAp = new DataTable();
            sql = "select * from tblCongDan WHERE cAp=N'"+cbAp.Text +"'";
            //dtChonAp = data.executeQueryTable(sql);
            paging = new MyDataPaging("tblCongDan", sql, "", "cQHChuHo", 50, GridViewdata, txtTrangHienTai, lbTongSoTrang);
            paging.Home();
            kiemTraCongDanViPham(lbMaCongDan.Text);
            hienthimauGridview();
        }
        
        public  void hienthimauGridview()
        {
            if (GridViewdata.Rows.Count > 0)
            {
                for (int ik = 0; ik < GridViewdata.Rows.Count; ik++)
                {
                    if (GridViewdata.Rows[ik].Cells["cHoTen"].Value.ToString().ToUpper().Equals(GridViewdata.Rows[ik].Cells["cSoHoKhau"].Value.ToString().ToUpper()))
                    {
                        GridViewdata.Rows[ik].DefaultCellStyle.ForeColor = Color.Blue ;
                    }
                    else if (GridViewdata.Rows[ik].Cells["Chet"].Value.ToString().ToUpper().Equals("TRUE"))
                    {
                        GridViewdata.Rows[ik].DefaultCellStyle.ForeColor = Color.DarkGray;
                    }
                    else if (GridViewdata.Rows[ik].Cells["ChuyenDiNgoaiXa"].Value.ToString().ToUpper().Equals("TRUE"))
                    {
                        GridViewdata.Rows[ik].DefaultCellStyle.ForeColor = Color.DarkKhaki;
                    }
                    else if ( GridViewdata.Rows[ik].Cells["cDiDau"].Value.ToString() != "")
                    {
                        GridViewdata.Rows[ik].DefaultCellStyle.ForeColor = Color.DarkKhaki;
                    }
                    else if(GridViewdata.Rows[ik].Cells["cODauDen"].Value.ToString() != "")
                    {
                        GridViewdata.Rows[ik].DefaultCellStyle.ForeColor = Color.Brown  ;
                    }
                    else
                    {
                        GridViewdata.Rows[ik].DefaultCellStyle.ForeColor = Color.Black;
                    }
                }
            }
        }
        private void txtAp_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Down)
            {
                txtSoNha.Focus();
            }
            if (e.KeyCode == Keys.Up)
            {
                txtDanToc.Focus();
            }
        }

        private void btnXuatExcel_Click(object sender, EventArgs e)
        {
            //try
            //{
            SaveFileDialog save = new SaveFileDialog();
            save.Title = "Chọn nơi lưu trữ";
            save.Filter = "Excel 2003 (*xls)| *xls";
            save.ShowDialog();
            if (save.FileName != "")
            {
                ExcelCOM.Application exApp = new ExcelCOM.Application();
                ExcelCOM.Workbook exBook = exApp.Workbooks.Add(ExcelCOM.XlWBATemplate.xlWBATWorksheet);
                ExcelCOM.Worksheet exSheet = (ExcelCOM.Worksheet)exBook.Worksheets[1];
                exSheet.Name = "Sheet1";

                progressBar1.Visible = true;
                progressBar1.Maximum = GridViewdata.RowCount;
                for (int cot = 1; cot < GridViewdata.Columns.Count-1; cot++)
                {
                    ExcelCOM.Range r = (ExcelCOM.Range)exSheet.Cells[1, cot];
                    r.ColumnWidth = 15;
                    r.Value2 = GridViewdata.Columns[cot].HeaderText.ToString();
                    for (int k = 0; k < GridViewdata.Rows.Count; k++)
                    {
                        ExcelCOM.Range r0d = (ExcelCOM.Range)exSheet.Cells[k + 2, cot ];
                        r0d.Value2 = "'" + GridViewdata.Rows[k].Cells[cot].Value.ToString();
                        progressBar1.Value = k;
                    }

                }
             
                progressBar1.Visible = false;

                exApp.Visible = false;
                exBook.SaveAs(save.FileName, ExcelCOM.XlFileFormat.xlWorkbookNormal,
                    null, null, false, false,
                    ExcelCOM.XlSaveAsAccessMode.xlExclusive,
                    false, false, false, false, false);
                exBook.Close(false, false, false);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(exBook);
                exApp.Quit();
                System.Runtime.InteropServices.Marshal.ReleaseComObject(exApp);
            }
            //}
            //catch
            //{

            //}
        }

        private void txtAp_Validated(object sender, EventArgs e)
        {
            txtAp.Text = txtAp.Text.ToUpper();
        }

        private void btnChuyenKhau_Click(object sender, EventArgs e)
        {
            
        }

        private void GridViewdata_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            //GridViewNhom.Rows[e.RowIndex].Cells["cMaDM"].Value.ToString();
            DataGridViewRow drSelected = GridViewdata.Rows[e.RowIndex];
            //if (GridViewdata.Columns[e.ColumnIndex].Name == "cHoTen")
            //{
            sql = "UPDATE tblCongDan SET cHoTen=N'" + 
                drSelected.Cells["cHoTen"].Value.ToString() + 
                "',cAp=N'" + drSelected.Cells["cAp"].Value.ToString() +
                "',cQHChuHo=N'" + drSelected.Cells["cQuanHeChuHo"].Value.ToString() +
                "',cGioiTinh=N'" + drSelected.Cells["cGioiTinh"].Value.ToString() +
                "',cNgaySinh=N'" + drSelected.Cells["cNgaySinh"].Value.ToString() +
                "',cNguyenQuan=N'" + drSelected.Cells["cNguyenQuan"].Value.ToString() +
                "',cNoiSinh=N'" + drSelected.Cells["cNoiSinh"].Value.ToString() +
                "',cSoCMND=N'" + drSelected.Cells["cSoCMND"].Value.ToString() +
                "',cDanToc=N'" + drSelected.Cells["cDanToc"].Value.ToString() +
                "',cSoNha=N'" + drSelected.Cells["cSoNha"].Value.ToString() +
                "',cTrinhDoHocVan= N'" + drSelected.Cells["cTrinhDo"].Value.ToString() +
                "',cTonGiao=N'" + drSelected.Cells["cTonGiao"].Value.ToString() +
                "',cNgheNghiep=N'" + drSelected.Cells["cNgheNghiep"].Value.ToString() +
                "',cSoHoKhau=N'" + drSelected.Cells["cSoHoKhau"].Value.ToString() +
                "',cSoSoHoKhau=N'" + drSelected.Cells["cSoSoHoKhau"].Value.ToString() +
                "',cODauDen=N'" + drSelected.Cells["cODauDen"].Value.ToString() +
                "',cNgayDen=N'" + drSelected.Cells["cNgayDen"].Value.ToString() +
                "',cDiDau=N'" + drSelected.Cells["cDiDau"].Value.ToString() +
                "',cNgayDi=N'" + drSelected.Cells["cNgayDi"].Value.ToString() +
                "',cChuyenDiCaHo=N'" + drSelected.Cells["cChuyenDiCaHo"].Value.ToString() +
                "',cGhiChu=N'" + drSelected.Cells["cGhiChu"].Value.ToString() +
                "', cChet=N'" + drSelected.Cells["Chet"].Value.ToString() + 
                "' WHERE cMaCongDan='" + lbMaCongDan.Text + "' ";
            
            
            data.executeNonQuery(sql);
            //btnRefresh_Click(sender, e);
            //MessageBox.Show(lbMaCongDan.Text);
            //MessageBox.Show("Cập nhật");
        }
        private void GridViewdata_Sorted(object sender, EventArgs e)
        {
            hienthimauGridview();
        }
       
    }
}
